package cn.itcast.bos.service.system.impl;

import cn.itcast.bos.dao.system.MenuRepositor;
import cn.itcast.bos.domain.system.Menu;
import cn.itcast.bos.domain.system.User;
import cn.itcast.bos.service.system.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class MenuServiceImpl implements MenuService {

    @Autowired
    private MenuRepositor menuRepositor;

    @Override
    public List<Menu> findAll() {
        List<Menu> all = menuRepositor.findAll();
        return all;
    }

    @Override
    public void save(Menu model) {
        //防止没有选中父菜单
        if(model.getParentMenu() != null && model.getParentMenu().getId() == 0){
            model.setParentMenu(null);
        }
        menuRepositor.save(model);
    }

    @Override
    public Menu findOne(int i) {
        return menuRepositor.findOne(i);
    }

    @Override
    public List<Menu> findMenuByUser(User user) {
        if (user.getUsername().equalsIgnoreCase("admin")){
            List<Menu> all = menuRepositor.findAll();
            return all;
        }else {
            return menuRepositor.findMenuById(user.getId());
        }
    }
}
